// pages/houseDetail/houseDetail.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    houseFlag:'',
    id: '',
    communityName: '',
    houseDetail:'',
    houseNum: 0,
    unfold: false,
    block: '',
    current:1,
    loadSuccess: false,
    city: '南京'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      houseFlag: options.flag,
      id: options.id,
      communityName: options.communityName
    })
    if (options.flag == 3){
      this.data.block = 3;
    }
    else{
      this.data.block = 2;
    }
    wx.setNavigationBarTitle({
      title: options.name,
    })
    if(wx.getStorageSync("city") == '连云港'){
      this.setData({
        city: '连云港'
      })
    }
    this.getDetailInfo();
    this.getNumInfo()
  },
  intervalChange: function(e){
    this.setData({
      current: e.detail.current + 1
    })
  },
  // 据小区名查询该小区下房源的数量
  getNumInfo: function(){
    var _this = this;
    if (_this.data.houseFlag == 3){
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseRental/search/communityCount',
        data: {
          communityName: _this.data.communityName,
          city: _this.data.city
          // houseType: _this.data.houseFlag
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              houseNum: res.data.data
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseSale/getCountsByCommunityName',
        data: {
          communityName: _this.data.communityName,
          houseType: _this.data.houseFlag
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              houseNum: res.data.data
            })
          }
        }
      })
    }
  },
  // 控制是否展开
  unfoldFunc: function(){
    this.setData({
      unfold: !this.data.unfold
    })
  },
  // 查看详情
  getDetailInfo: function(){
    var _this = this;
    if (_this.data.houseFlag == 3) {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseRental/getTGyHouseRental',
        data: {
          id: _this.data.id,
          userId: JSON.parse(wx.getStorageSync('userInfo')).id
        },
        success: function (res) {
          _this.setData({
            loadSuccess: true
          })
          wx.hideLoading()
          if (res.data.success) {
            var houses = res.data.data;
            if (houses.picture) {
              houses.picture = houses.picture.split(";");
            }
            if (houses.housingCharacteristics) {
              houses.housingCharacteristics = houses.housingCharacteristics.split(",");
            }
            houses.releaseTime = util.formatTime(houses.releaseTime);
            if (houses.housingDescription) {
              if (houses.housingDescription.length > 70){
                houses.housingDescriptionShort = houses.housingDescription.substring(0, 70) + "...";
              }
              else{
                _this.setData({
                  unfold: true
                })
              }
            }
            _this.setData({
              houseDetail: houses
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseSale/getHouseSaleDetailById',
        data: {
          id: _this.data.id,
          userId: JSON.parse(wx.getStorageSync('userInfo')).id
        },
        success: function (res) {
          _this.setData({
            loadSuccess: true
          })
          wx.hideLoading()
          if (res.data.success) {
            var houses = res.data.data;
            if (houses.picture) {
              houses.picture = houses.picture.split(",");
            }
            if (houses.housingCharacteristics) {
              houses.housingCharacteristics = houses.housingCharacteristics.split(",");
            }
            houses.releaseTime = util.formatTime(houses.releaseTime);
            if (houses.housingDescription) {
              if (houses.housingDescription.length > 70) {
                houses.housingDescriptionShort = houses.housingDescription.substring(0, 70) + "...";
              }
              else {
                _this.setData({
                  unfold: true
                })
              }
            }
            _this.setData({
              houseDetail: houses
            })
          }
        }
      })
    }
  },
  // 收藏
  collect: function(e){
    var coll = e.currentTarget.dataset.coll;
    var _this = this;
    if(coll == 1){
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/deleteCollectionRecordByParams',
        data: {
          sourceId: _this.data.id,
          userId: JSON.parse(wx.getStorageSync('userInfo')).id,
          block: _this.data.block
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            if (_this.data.houseFlag == 3) {
              _this.setData({
                ['houseDetail.isCollection']: 0
              })
            }
            else {
              _this.setData({
                ['houseDetail.collentionCount']: 0
              })
            }
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/save',
        data: {
          sourceId: _this.data.id,
          userId: JSON.parse(wx.getStorageSync('userInfo')).id,
          block: _this.data.block
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 2000
            })
            if (_this.data.houseFlag == 3){
              _this.setData({
                ['houseDetail.isCollection']: 1
            })
            }
            else{
              _this.setData({
                ['houseDetail.collentionCount']: 1
              })
            }
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  // 打电话
  toggleSelect: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      //current_tel: e.currentTarget.dataset.tel
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value
    })
    this.setData({
      showSelect: false
    })
  },
  // 小区房源信息
  enterHouse: function(e){
    var item = e.currentTarget.dataset.item;
    if (this.data.houseFlag == 3){
      wx.navigateTo({
        url: '/pages/houseSearch1/houseSearch1?flag=' + this.data.houseFlag + "&communityName=" + item.communityName
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/houseSearch/houseSearch?flag=' + this.data.houseFlag + "&communityName=" + item.communityName
      })
    }
  },
  // 发布人信息
  enterSeller: function (e) {
    var item = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/houseSeller/houseSeller?flag=' + this.data.houseFlag + "&releaseUserId=" + e.currentTarget.dataset.userid + "&item=" + item
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})